package com.sky.service.impl;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.interceptor.JwtTokenAdminInterceptor;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImp implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorsMapper dishFlavorsMapper;

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.select(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public void save(DishDTO dishDTO) {

            Dish dish = new Dish();
            BeanUtils.copyProperties(dishDTO,dish);
            dish.setCreateTime(LocalDateTime.now());
            dish.setUpdateTime(LocalDateTime.now());
            dish.setCreateUser(JwtTokenAdminInterceptor.threadLocal.get());
            dish.setUpdateUser(JwtTokenAdminInterceptor.threadLocal.get());
            dishMapper.save(dish);

            Long id = dish.getId();
            List<DishFlavor> dishFlavors = new ArrayList<>();
            dishFlavors = dishDTO.getFlavors();
            if (!dishFlavors.isEmpty())
            {
                dishFlavors.forEach(s->s.setDishId(id));
                dishFlavorsMapper.save(dishFlavors);
            }

    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
       Integer start =  (dishPageQueryDTO.getPage()-1)*dishPageQueryDTO.getPageSize();
       Integer pagesize = dishPageQueryDTO.getPageSize();
       dishPageQueryDTO.setStart(start);
       List<DishVO> dishVO = dishMapper.page(dishPageQueryDTO);
       Integer count = dishMapper.count();
       return new PageResult(count,dishVO);
    }

    @Override
    public void delete(List<Long> ids) {
        for (Long i:ids)
        {
            Dish dish  = dishMapper.selectById(i);
            if(dish.getStatus()==1)
            {
                throw new BaseException(dish.getName()+"正在售卖，不能删除");
            }
        }
        dishMapper.delete(ids);
        dishFlavorsMapper.delete(ids);
    }

    @Override
    public DishVO select(Long id) {
        //根据id把菜品查询出来
       Dish dish  = dishMapper.selectById(id);
       DishVO dishVO = new DishVO();
       BeanUtils.copyProperties(dish,dishVO);

        //再把通过id查询味道
        List<DishFlavor> dishFlavors= dishFlavorsMapper.select(id);
        dishVO.setFlavors(dishFlavors);
        //返回数据
        return dishVO;
    }

    @Override
    public void update(DishVO dishVO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO, dish);
        //修改菜品
        dishMapper.update(dish);
        //先删除口味
        if (dishVO.getFlavors()!=null)
        {
            dishFlavorsMapper.delects(dishVO.getId());

            List<DishFlavor> dishFlavor  = dishVO.getFlavors();

            dishFlavor.forEach(s->s.setDishId(dishVO.getId()));
            dishFlavorsMapper.save(dishFlavor);
        }


        //再加入口味


    }

    @Override
    public void setstatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    @Override
    public List<Dish> selectcateid(Long categoryId) {
        List<Dish> list = dishMapper.selectcateid(categoryId);
        return list;
    }

}
